<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl"/>
    <script src="/js/plugins/ckeditor/ckeditor.js"></script>
    <script>
        $(function () {

            //引入文本编辑器插件ckeditor
            var ck = CKEDITOR.replace( 'editor' );
                $(".inputBtn").click(function () {

                    //1.清空模态框数据
                    $("#editForm input,#editForm select").val("");
                    $("#coverUrl").attr("src","");


                    //二级联动 给所攻略绑定值改变事件
                    $("#strategySelect").change(function () {
                        var strategyId = $(this).val();
                        if (strategyId) {
                            $.get("/strategyCatalog/listByStrategyId.do",{strategyId:strategyId},function (data) {
                                console.log(data);
                                //拼接
                                temp="";
                                $.each(data,function (index,ele) {
                                    temp += "<option value='"+ele.id+"'>"+ele.name+"</option>";
                                });
                                //添加到下拉框中
                                $("#catalogSelect").html(temp);
                            });
                        }
                    });





                    //获取绑定的json
                    var json = $(this).data('json');
                    console.log(json);
                    // 回显
                    if(json){
                        //回显id隐藏域
                        $("input[name='id']").val(json.id);
                        //标题
                        $("input[name='title']").val(json.title);
                        //序号
                        $("input[name='sequence']").val(json.sequence);
                        //封面
                        $("#coverUrl").attr("src",json.coverUrl);
                        $("input[name='coverUrl']").val(json.coverUrl);
                        //状态
                        $("select[name='state']").val(json.state);

                        //所属攻略
                        $("select[name='strategyId']").val(json.strategyId);


                        //回显小攻略 攻略分类
                        $.get("/strategyCatalog/listByStrategyId.do",{strategyId:json.strategyId},function (data) {
                            console.log(data);
                            //拼接
                            temp="";
                            $.each(data,function (index,ele) {
                                temp += "<option value='"+ele.id+"'>"+ele.name+"</option>";
                            });
                            //添加到下拉框中
                            $("#catalogSelect").html(temp);
                            //攻略类别
                            $("select[name='catalog.id']").val(json.catalogId);
                        });
                        
                        //查询攻略文章内容
                        $.get("/strategyDetail/getContentById.do",{id:json.id},function (data) {
                            console.log(data);
                            //设置到文本编辑器中
                            ck.setData(data.content);
                        });


                    }else{
                        //保存时 清除富文本编辑器
                        ck.setData("");
                    }
                    //回显模态框
                    $("#inputModal").modal("show");

                    //保存按钮
                    $("#editForm").ajaxForm(function (data) {
                        reload();
                    })
                    $(".btn-submit").click(function () {
                        //1.文本编辑器中获取内容
                        var content = ck.getData();
                        //2.设置form表单textarea
                        $("textarea[name='strategyContent.content']").val(content);
                        $("#editForm").submit();
                    });
                });






        });
    </script>


</head>
<body>

<div class="container " style="margin-top: 20px">
   <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#assign currentMenu="strategyDetail"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label for="keyword">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入攻略标题">
                </div>
                <button id="btn_query" class="btn btn-primary">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a class="btn btn-success  btn-input inputBtn"  >
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>

            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                </tr>
                </thead>
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index+1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" style="width: 30px"/></td>
                        <td>${(entity.releaseTime?string("yyyy-MM-dd HH:mm:ss"))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>${entity.stateName}</td>
                        <td>
                            <a class="btn btn-info btn-xs btn-input inputBtn" data-json='${entity.json}' >
                                <span class="glyphicon glyphicon-pencil"></span>修改
                            </a>
                        </td>

                    </tr>
                </#list>
            </table>
           <#include "../common/page.ftl"/>
        </div>
    </div>
</div>



<#--修改模态框-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新增大攻略</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm" enctype="multipart/form-data">
                    <input type="hidden" name="id" >
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="title" name="title" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="coverUrl" width="100%"/>
                            <input type="file" class="form-control" name="file" >
                            <input type="hidden" class="form-control" name="coverUrl" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="strategySelect" name="strategyId">
                                <#list strategies as strategy>
                                    <option value="${strategy.id}">${strategy.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="catalogSelect" name="catalog.id"  >

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select id="auditState" class="form-control" autocomplete="off" name="state" >
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">序号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sequence" name="sequence" >
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="strategyContent.content" id="editor" rows="10" cols="80">
                        </textarea>

                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


</body>
</html>